(function () {
    var EVENT_CLICK = Laya.Event.CLICK;
    var io = ppt.io;

    function roleBtn(call, type) {
        roleBtn.super(this);
        this.callback = call;
        this._chooDetails = null;
        this.data = null;
        this.type = type;
        this.init();
    }
    Laya.class(roleBtn, "Com.Role.Btn", ui.Role.RoleBtnUI);
    var _proto = roleBtn.prototype;

    _proto.init = function () {
        this.creatUI();
        this.bindEvent();
    }
    _proto.bindEvent = function () {
        this.starBtn.on(Laya.Event.CLICK, this, function () {
            io.emit({ 'cmd': 'role::advance', params: { 'roleId': this.data.roleId } });

        });
    }
    _proto.creatUI = function () {
        var clipIndex;
        if (this.type == 0) {
            clipIndex = 1;
        } else if (this.type == 1) {
            clipIndex = 0;
        }
        else if (this.type == 2) {
            clipIndex = 3;
        }
        else if (this.type == 3) {
            clipIndex = 2;
        }
        var figureDetails = [];
        //黑妞龙骨动画
        figureDetails[0] = utils.createSkeleton('other/role/normal/1001');
        //皮蛋龙骨动画
        figureDetails[1] = utils.createSkeleton('other/role/normal/1002');
        //猫咪龙骨动画
        figureDetails[2] = utils.createSkeleton('other/role/normal/1003');
        //老虎龙骨动画
        figureDetails[3] = utils.createSkeleton('other/role/normal/1004');
        this._chooDetails = figureDetails[this.type];
        this._chooDetails.scale(0.8, 0.8);
        this._chooDetails.play(0, true);
        this._chooDetails.pos(128, 32);
        this.starBtn.addChild(this._chooDetails);
        this.debrisCh.index = clipIndex;
    }
    //升星请求数据
    _proto.moneyData = function (data) {
        if (data.code) {
            var config = {
                msg: data.msg,
                close: true,
                btn: {
                    btnCancel: {
                        skin: "cancel"
                    },
                    btnConfirm: {
                        skin: "confirm"
                    }
                }
            }
            switch (data.code) {
                case 302:
                    config.btn = {
                        btnCancel: false,
                        btnConfirm: {
                            skin: "confirm",
                            click: function () {
                                ppt.director.runScene(new Scene.Shop);
                            }
                        }
                    }
                    break;
                case 303:
                    config.btn = {
                        btnCancel: {
                            skin: "challenge",
                            click: function () {
                                io.emit({ cmd: 'pve::wait', params: { type: 6 } });
                            }
                        },
                        btnConfirm: {
                            skin: "race",
                            click: function () {
                                ppt.director.runScene(new Scene.Matching);
                            }
                        }
                    }
                    break;
                case 321:
                    config.btn = {
                        btnCancel: false,
                        btnConfirm: {
                            skin: "challenge",
                            click: function () {
                                io.emit({ cmd: 'pve::wait', params: { type: 6 } });
                            }
                        }
                    }
                    break;
            }
            ppt.director.popScene(new Alert.Public(config));
            return;
        } else {
            var length = data.role.advance.length;
            if (data.role.star > 4) {
                this.manStar.visible = true;
                this.starHaveS.visible = false;
            }
        }
        var starLength = [];
        var skillNum = null;
        var chipNum = null;
        var roleup = utils.createSkeleton('res/role/up');
        this.starBtn.addChild(roleup);
        roleup.zOrder = 4;
        roleup.pos(-840, -100);
        roleup.play('roleup', false);
        roleup.once(Laya.Event.STOPPED, roleup, function () {
            this.visible = false;
        });

        if (data.role.star < 5) {
            this.starHave.text = data.role.advance[data.role.star - 1].chip + '+';
            this.starMoney.text = data.role.advance[data.role.star - 1].cost;
        }

        for (var j = 0; j < 5; j++) {
            if (j < data.role.star) {
                starLength[j] = { 'trend': 1 };

            } else {
                starLength[j] = { 'trend': 0 };
            }
        }
        skillNum = data.role.star;
        chipNum = data.role.chip;
        io.emit({ 'cmd': 'game::enter', params: {} });
        this.callback(starLength, skillNum, chipNum);
    }
    _proto.btnData = function (data) {
        this.data = data;
        if (this.data.status == 0 || this.data.status == 1) {
            this.noHave.visible = false;
            if (this.data.star > 4) {
                this.manStar.visible = true;
                this.starHaveS.visible = false;
            } else {
                this.starHaveS.visible = true;
                this.manStar.visible = false;
            }
        } else {
            this.starHaveS.visible = false;
            this.manStar.visible = false;
            this.noHave.visible = true;
        }
        for (var j = 0; j < this.data.advance.length; j++) {
            if (this.data.star == (this.data.advance[j].star - 1)) {
                this.starHave.text = this.data.advance[j].chip + '+';
                this.starMoney.text = this.data.advance[j].cost;
            }
        }
    }
})();